#include "PayBox.h"
#include <Arduino.h>
#include <ArduinoOTA.h>
#include <TFT_eSPI.h>
#include <lv_demo.h>

// 定义TFT
TFT_eSPI tft = TFT_eSPI();
char buf[32] = {0};
long check5Ms = 0;
long check1s = 0;
hw_timer_t *tm = nullptr;

void setup() {
  // 初始化串口
  Serial.begin(115200);
  // LVGL初始化
  lv_init();
  // 初始化TFT
  payTFTInit(100);
  autoConfigWifi();
  tft.println("Wifi Connected!");
  sprintf(buf, "IP: %s", WiFi.localIP().toString().c_str());
  tft.println(buf);
  // 设置系统时间同步
  configTime(TZ_SEC, DST_SEC, "ntp.ntsc.ac.cn", "ntp1.aliyun.com");
  delay(2000);
  // 初始化LV配置
  setupLvDisplay();

  tm = timerBegin(0, 80, true);
  timerAttachInterrupt(
      tm, [] { lv_tick_inc(1); }, true);
  timerAlarmWrite(tm, 1000, true);
  timerAlarmEnable(tm);

  lv_demo_widgets();

  setupOTAConfig();
}

void loop() {
  auto ms = millis();
  if (ms - check5Ms > 5) {
    check5Ms = ms;
    lv_task_handler();
  }
  if (ms - check1s > 1000) {
    check1s = ms;
    ArduinoOTA.handle();
  }
}
